package com.qdxwx.manage.controller.course;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.course.CoursePackageService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.course.Course;
import com.qdxwx.models.course.CourseDTO;
import com.qdxwx.models.course.CourseQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 课程Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/course/package")
public class CoursePackageController extends BaseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private CoursePackageService coursePackageService;

    /**
     * 套餐课程列表
     */
    @GetMapping("/list.html")
    public String index(HttpServletRequest request, ModelMap mmpan) {
        int courseId = TKConvert.toInteger(request.getParameter("courseId"));
        if (courseId < 1) return error500;
        mmpan.put("courseId", courseId);
        return "course/package-list";
    }

    /**
     * 查询课程列表
     */
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo courseList(HttpServletRequest request) {
        String courseName = request.getParameter("courseName");
        int courseId = TKConvert.toInteger(request.getParameter("courseId"));
        Course course = new Course();
        course.setCourseId(courseId);
        course.setCourseName(courseName);
        List<CourseDTO> list = courseService.getPackageList(course, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(courseService.getPackageCount(course));
        return rspData;
    }

    /**
     * 套餐课程列表
     */
    @GetMapping("/select/list.html")
    public String selectList(HttpServletRequest request, ModelMap mmpan) {
        int courseId = TKConvert.toInteger(request.getParameter("courseId"));
        if (courseId < 1) return error500;
        Course course = courseService.get(courseId);
        if (course == null) return error500;
        mmpan.put("channelId", course.getChannelId());
        mmpan.put("courseId", courseId);
        return "course/package-course-select";
    }

    /**
     * 查询课程列表
     */
    @PostMapping("/select/list.json")
    @ResponseBody
    public TableDataInfo courseSetmealAddList(HttpServletRequest request) {
        int courseId = TKConvert.toInteger(request.getParameter("courseId"));
        int channelId = TKConvert.toInteger(request.getParameter("channelId"));
        String courseName = request.getParameter("courseName");
        CourseQuery query = new CourseQuery();
        query.setCourseId(courseId);
        query.setChannelId(channelId);
        query.setIsAvailable(1);
        query.setSellTypeExclude("PACKAGE");
        query.setCourseName(courseName);
        List<Course> list = courseService.getList(query, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(courseService.count(query));
        return rspData;
    }

    /**
     * 关联课程列表保存
     */
    @PostMapping("/save.json")
    @ResponseBody
    public AjaxResult courseSetmealSave(HttpServletRequest request) {
        int courseId = TKConvert.toInteger(request.getParameter("courseId"));
        String[] childIds = TKConvert.toStrArray(request.getParameter("childIds"));
        if (courseId < 1) return AjaxResult.error("关联套餐id错误");
        if (childIds.length < 1) return AjaxResult.error("至少选择一条课程");
        for (String childId : childIds) {
            coursePackageService.insert(courseId, TKConvert.toInteger(childId));
        }
        return AjaxResult.success();
    }

    /**
     * 关联课程列表删除
     */
    @PostMapping("/remove.json")
    @ResponseBody
    public AjaxResult courseSetmealremove(HttpServletRequest request) {
        int parentId = TKConvert.toInteger(request.getParameter("parentId"));
        int courseId = TKConvert.toInteger(request.getParameter("courseId"));
        if (parentId < 1 || courseId < 1) return AjaxResult.error("参数异常");

        int row = coursePackageService.delete(parentId, courseId);
        return row > 0 ? AjaxResult.success() : AjaxResult.error("删除失败");
    }

}
